#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int lengthOfLongestSubsequence(vector<int>& nums, int target) {
        int n = nums.size();
        vector<int> dp(target + 1, INT_MIN);
        dp[0] = 0;
        int pathmax = 0;
        for (auto x : nums) {
            pathmax = min(pathmax + x, target);
            for (int j = pathmax; j >= x; j--) {
                dp[j] = max(dp[j], dp[j - x] + 1);
            }
            //for(int j = 0; j <= target; j++) cout << dp[j] << ' ';
            //cout << endl;
        }
        return (dp[target] > 0 ? dp[target] : -1);
    }
};